package com.swh.controller;

import com.swh.entity.Article;
import com.swh.servic.HomeService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/26 14:30
 * @Version: 1.0
 */
@WebServlet("/home/*")
public class HomeServlet extends BaseServlet {
    private HomeService homeService = new HomeService();

    //首页信息
    public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 查询 今日新注册用户、新发布文章、新发布会议等
        Map<String, Object> countData = homeService.findHomeCount();

        // 查询近7日新注册用户、新发布文章、新发布会议
        List<Map<String, Object>> detailData = homeService.findHomeDetail();

        // 转换为集合方式便于前端取出
        List<List> countList = new ArrayList<>();

        for (Map<String, Object> map : detailData) {
            List temo = new ArrayList();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                Object val = map.get(entry.getKey());
                temo.add(val);
            }
            countList.add(temo);

        }
        Map<String,Object> data=new HashMap<>();

        // count数据
        data.put("count",countData);

        // 报表详细数据
        data.put("detail",countList);

        writeObjToString(response,data);
    }
}
